package handlers

import (
	"2-11zy/api/request"
	"2-11zy/api/response"
	"2-11zy/dao"
	"github.com/gin-gonic/gin"
)

func AddRoom(c *gin.Context) {
	userId := c.GetUint("userId")
	var data request.AddRoom
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	result := dao.AddRoom(int(userId))
	response.ResponseSuccess(c, result.ID)
}

func AddUserRooms(c *gin.Context) {
	var data request.AddRoom
	err := c.ShouldBind(&data)
	if err != nil {
		response.ResponseError(c, err.Error())
		return
	}
	if !dao.IsExitsUserIds(data.RoomUserId) {
		response.ResponseError(c, "用户不存在")
		return
	}
	if !dao.IsExitsRoomByRoomId(data.RoomId) {
		response.ResponseError(c, "群不存在")
		return
	}
	userId := c.GetUint("userId")
	ru := dao.GetRoomByRoomId(data.RoomId, int(userId))
	if ru.ULevel != 1 && ru.ULevel != 2 {
		response.ResponseError(c, "你不配")
		return
	}
	if !dao.CreateRoomUser(data.RoomUserId, data.RoomId) {
		response.ResponseError(c, "添加错误")
		return
	}
	response.ResponseError(c, "成功")
}
